/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hirt;

import java.nio.ByteBuffer;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * PlayerchangeAction are used whenever a player wants to change his name of
 * add/remove roles
 * @author zamille
 */
public class PlayerChangeAction extends Action
{
    String newName;
    Role newRole;
    byte type;

    public static final byte CHANGE_NAME = 1;
    public static final byte ADD_ROLE = 2;
    public static final byte REMOVE_ROLE = 3;

    /**
     *
     * @param actor
     * @param newName
     */
    public PlayerChangeAction(Player actor, String newName)
    {
        super(actor);
        this.newName = newName;
        type = CHANGE_NAME;
        newRole = null;
        super.messageType = Message.PLAYERCHANGE_MESSAGE;
    }

    /**
     *
     * @param actor
     * @param newRole
     */
    public PlayerChangeAction(Player actor, int newRole)
    {
        super(actor);
        this.newRole = new Role(newRole);
        if (actor.containsRole(newRole))
            type=REMOVE_ROLE;
        else
            type=ADD_ROLE;
        newName = null;
        super.messageType = Message.PLAYERCHANGE_MESSAGE;
    }

    /**
     *
     * @param actor
     * @param newRole
     */
    public PlayerChangeAction(Player actor, Role newRole)
    {
        super(actor);
        this.newRole = newRole;
        if (actor.containsRole(newRole.getId()))
            type=REMOVE_ROLE;
        else
            type=ADD_ROLE;
        newName = null;
        super.messageType = Message.PLAYERCHANGE_MESSAGE;
    }

    /**
     * Builds an Action from a byte stream, presumably created by an action across the network
     * this action should be identical to that one
     * @param bytes
     * @param items: Master Item List used to build the action
     */
    public PlayerChangeAction(ByteBuffer bytes, Vector<Item> items)
    {
        fromBytes(bytes,items);
        super.messageType = Message.PLAYERCHANGE_MESSAGE;
        
    }

    /**
     * Builds an Action from a byte stream, presumably created by an action across the network
     * this action should be identical to that one
     * @param bytes
     * @param items: Master Item List used to build the action
     * @return what's left of the parem bytes after this methods completes
     */
    @Override
    protected ByteBuffer fromBytes(ByteBuffer bytes, Vector<Item> items)
    {
        bytes = super.fromBytes(bytes, items);   //it uses the inherited method to build
        type = bytes.get(0);                 //the inherited members from the ByteBuffer
        int length = 0;
        if (type == CHANGE_NAME)
        {
           length = bytes.getInt(1);
           newName = ByteChanger.getString(bytes, 5,length);

        }
        else
        {
            newRole = new Role(bytes.getInt(1));
        }
        try {
            bytes = ByteChanger.truncate(bytes, 5 + length);
        } catch (Exception ex) {

        }
        return bytes;
    }

    /**
     * Returns a ByteBuffer representation of this Action
     * Used to send actions over the network
     * @return ByteBuffer
     */
    @Override
    public ByteBuffer toByte()
    {
        ByteBuffer buf = super.toByte();   //it uses the inherited method to put
        buf.put(type);                  //the inherited members in the ByteBuffer
        if (type == CHANGE_NAME)
        {
            buf.putInt(newName.length());
            buf.put(ByteChanger.stringToByte(newName));
        }
        else
        {
            buf.putInt(newRole.getId());
        }
        return buf;
    }

    public byte getType()
    {
        return type;
    }
}
